package com.gaojx.lang.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.rmi.runtime.Log;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author junxiang.gao
 * @since 2020/8/19
 */
public class FileAndFolderUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileAndFolderUtil.class);

    /**
     * 获取项目加载的根路径
     *
     * @return
     */
    public static String getClassPath() {
        String strRes = "";

        try {
            strRes = FileAndFolderUtil.class.getResource("/").toURI().getPath();

            URL url = FileAndFolderUtil.class.getResource("/");
            LOGGER.error("FileAndFolderUtil url = {}", url);

            URI uri = url.toURI();
            LOGGER.error("FileAndFolderUtil uri = {}", uri);

            String path = uri.getPath();
            LOGGER.error("FileAndFolderUtil path = {}", path);

        } catch (URISyntaxException e) {
            LOGGER.error("getClassPath: URISyntaxException: {}", e.getMessage());
        }
        LOGGER.info("getClassPath(): strRes = {}", strRes);
        return strRes;
    }

    /**
     * 获取文件夹下所有指定后缀的文件名列表
     *
     * @param strFolderPath 文件夹路径
     * @param strSuffix     需要比那里的文件后缀
     * @param isAbsPath     是否使用绝对路径返回
     * @return
     */
    public static List<String> getFileName(String strFolderPath, String strSuffix, boolean isAbsPath) {
        List<String> filenameList = new ArrayList<>();

        // 当前查找文件目录的File对象
        File fileFolderDir;

        if (strFolderPath == null || "".equals(strFolderPath)) {
            LOGGER.error("文件夹路径为空!");
            return null;
        }

        LOGGER.debug(">>>>>>>>>>>> strFolderPath = {}", strFolderPath);

        String subFolderPath;
        // 判断操作系统类型
        if ("/".equals(File.separator)) {
            subFolderPath = strFolderPath.substring(strFolderPath.length() - 1);
        } else {
            subFolderPath = strFolderPath.substring(strFolderPath.length() - 2);
        }
        LOGGER.debug(">>>>>>>>>>>> subFolderPath = {}", subFolderPath);

        // 以当前文件夹生成文件对象
        fileFolderDir = new File(subFolderPath);

        String[] filenameArray = fileFolderDir.list();

        if (filenameArray == null || filenameArray.length < 1) {
            LOGGER.error("该文件夹={}=下无任何文件", strFolderPath);
            return null;
        }

        StringBuilder newFilename = new StringBuilder(strFolderPath + File.separator);
        for (String filename : filenameArray) {
            if (filename.length() <= strSuffix.length()) {
                continue;
            }
            if (!filename.substring(filename.length() - strSuffix.length()).equals(strSuffix)) {
                continue;
            }


            if (isAbsPath) {
                newFilename.append(filename);
                filenameList.add(newFilename.toString());
                newFilename.delete(newFilename.lastIndexOf(File.separator)+1, newFilename.length());
            } else{
                filenameList.add(filename);
            }
        }

        return filenameList;
    }

}
